package com.trinity.web.controller.payment.unique;

import com.trinity.common.annotation.Log;
import com.trinity.common.core.controller.BaseController;
import com.trinity.common.core.domain.AjaxResult;
import com.trinity.common.core.page.TableDataInfo;
import com.trinity.common.enums.BusinessType;
import com.trinity.common.utils.poi.ExcelUtil;
import com.trinity.uniquefees.domain.bo.UfCourseBook;
import com.trinity.uniquefees.domain.vo.UfCourseBookVo;
import com.trinity.uniquefees.service.IUfCourseBookService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 教材信息Controller
 *
 * @author 董诗攀
 * @date 2023-12-11
 */
@RestController
@RequestMapping("/uniquefees/book")
public class UfCourseBookController extends BaseController {
    @Resource
    private IUfCourseBookService ufCourseBookService;

    /**
     * 查询教材信息列表
     */
    @PreAuthorize("@ss.hasPermi('uniquefees:book:list')")
    @GetMapping("/list")
    public TableDataInfo list(UfCourseBook ufCourseBook) {
        startPage();
        List<UfCourseBook> list = ufCourseBookService.selectUfCourseBookList(ufCourseBook);
        return getDataTable(list);
    }

    /**
     * 导出教材信息列表
     */
    @PreAuthorize("@ss.hasPermi('uniquefees:book:export')")
    @Log(title = "教材信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UfCourseBook ufCourseBook) {
        List<UfCourseBook> list = ufCourseBookService.selectUfCourseBookList(ufCourseBook);
        ExcelUtil<UfCourseBook> util = new ExcelUtil<UfCourseBook>(UfCourseBook.class);
        util.exportExcel(response, list, "教材信息数据");
    }

    /**
     * 获取教材信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('uniquefees:book:query')")
    @GetMapping(value = "/{courseId}")
    public AjaxResult getInfo(@PathVariable("courseId") Long courseId) {
        return success(ufCourseBookService.selectUfCourseBookByCourseId(courseId));
    }

    /**
     * 新增教材信息
     */
    @PreAuthorize("@ss.hasPermi('uniquefees:book:add')")
    @Log(title = "教材信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UfCourseBook ufCourseBook) {
        return toAjax(ufCourseBookService.insertUfCourseBook(ufCourseBook));
    }

    /**
     * 修改教材信息
     */
    @PreAuthorize("@ss.hasPermi('uniquefees:book:edit')")
    @Log(title = "教材信息", businessType = BusinessType.UPDATE)
    @PostMapping("/put")
    public AjaxResult edit(@RequestBody UfCourseBook ufCourseBook) {
        return toAjax(ufCourseBookService.updateUfCourseBook(ufCourseBook));
    }

    /**
     * 删除教材信息
     */
    @PreAuthorize("@ss.hasPermi('uniquefees:book:remove')")
    @Log(title = "教材信息", businessType = BusinessType.DELETE)
    @PostMapping("/{courseIds}/delete")
    public AjaxResult remove(@PathVariable Long[] courseIds) {
        return toAjax(ufCourseBookService.deleteUfCourseBookByCourseIds(courseIds));
    }
    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<UfCourseBookVo> util = new ExcelUtil<>(UfCourseBookVo.class);
        util.importTemplateExcel(response, "选课课程信息");
    }

    /**
     * 导入
     * @param file
     * @param listId 缴费项目
     * @return
     * @throws Exception
     */
    @Log(title = "教材信息导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('uniquefees:book:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, String listId) throws Exception
    {
        ExcelUtil<UfCourseBookVo> util = new ExcelUtil<>(UfCourseBookVo.class);
        List<UfCourseBookVo> list = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = ufCourseBookService.importPaymentItem(list, listId, operName);
        return success(message);
    }
}
